package com.banshuibao.bsb_android.view;

import android.content.Context;
import android.view.Gravity;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.banshuibao.bsb_android.R;
import com.ldf.calendar.Utils;
import com.ldf.calendar.component.State;
import com.ldf.calendar.interf.IDayRenderer;
import com.ldf.calendar.model.CalendarDate;
import com.ldf.calendar.view.DayView;

import java.util.HashMap;

/**
 * Created by zyh on 2018/5/18.
 */

public class CustomDayView extends DayView {

    private TextView dayText;

    private LinearLayout maker_layout;

    private CalendarDate today = new CalendarDate();

    public CustomDayView(Context context, int layoutResource) {
        super(context, layoutResource);
        dayText = findViewById(R.id.day);
        maker_layout = findViewById(R.id.maker_layout);
    }

    @Override
    public void refreshContent() {
        setDayText(day.getDate(), day.getState());
        setMaker_layout(day.getDate());
        super.refreshContent();
    }

    private void setMaker_layout(CalendarDate date) {
        HashMap<String, String> map = Utils.loadMarkData();
        maker_layout.removeAllViews();
        if (map.containsKey(date.toString())) {
            String mark = map.get(date.toString());
            String[] strs = mark.split("_");
            int size = Integer.valueOf(strs[0]);
            String color = strs[1];
            String[] colors = color.split(",");
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
            params.gravity = Gravity.CENTER | Gravity.BOTTOM;
            params.setMargins(2, 0, 2, 0);
            for (int i = 0; i < size; i++) {
                ImageView imageView = new ImageView(context);
                imageView.setLayoutParams(params);
                int j = i >= colors.length ? 0 : i;
                imageView.setImageResource(Integer.valueOf(colors[j]));
                maker_layout.addView(imageView);
            }
        }
    }

    private void setDayText(CalendarDate date, State state) {
        if (date != null) {
            if (state == State.NEXT_MONTH || state == State.PAST_MONTH) {
                dayText.setTextColor(context.getResources().getColor(R.color.text_default));
                dayText.setBackgroundResource(0);
                dayText.getPaint().setFakeBoldText(false);
            } else {
                dayText.getPaint().setFakeBoldText(true);
                if (date.equals(today)) {
                    dayText.setBackgroundResource(R.drawable.icon_calendar_day_bg);
                    dayText.setTextColor(context.getResources().getColor(R.color.white));
                } else {
                    dayText.setBackgroundResource(0);
                    dayText.setTextColor(context.getResources().getColor(R.color.text_black));
                }
            }
            dayText.setText(String.valueOf(date.getDay()));
        }
    }

    @Override
    public IDayRenderer copy() {
        return new CustomDayView(context, layoutResource);
    }
}
